package com.lx.huimailf.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.widget.EditText;
import android.widget.Toast;

import com.lx.huimailf.MyApplication;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import okhttp3.Call;

public class Tools {

	private static String fileDir;


	/**
	 * @param context
	 * @param fileName
	 * @param fileId
	 */
	public static void download(final Context context, final String fileName,
								final String fileId) {

		download(context, "/uploadfile", fileName, fileId);
	}

	/**
	 * 得到文件目录
	 *
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static void download(final Context context, final String folder,
								final String fileName, final String fileId) {
		fileDir = null;
		String dirName = Environment.getExternalStorageDirectory() + "/JNLX/";
		File f = new File(dirName);
		if (!f.exists()) {
			f.mkdir();
		}

		fileDir = dirName + fileName;
		final File file = new File(fileDir);
		// 如果目标文件已经存在，则删除。产生覆盖旧文件的效果
		if (!file.exists()) {
			System.out.println("重命名和普通》》》" + fileDir);
			// 在这可以下载了。。。。。
			downloadFile(context, folder, fileDir, fileId, fileName);
		} else {
			Dialog dialog = new AlertDialog.Builder(context).setTitle("警告！！")
					.setMessage("文件：" + fileName + "\n" + "已存在是否覆盖？")
					.setNegativeButton("是", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							file.delete();
							downloadFile(context, folder, fileDir, fileId,
									fileName);
							System.out.println("覆盖》》》" + fileDir);

						}
					}).setNeutralButton("否", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {

							// 重新命名的弹窗
							final EditText ed = new EditText(context);
							ed.setText(fileName);
							new AlertDialog.Builder(context)
									.setTitle("请输入新的文件名")
									.setIcon(android.R.drawable.ic_dialog_info)
									.setView(ed)
									.setNegativeButton("确定",
											new OnClickListener() {
												@Override
												public void onClick(
														DialogInterface dialog,
														int which) {
													download(context, ed
															.getText()
															.toString(), fileId);

												}
											})
									.setPositiveButton("取消",
											new OnClickListener() {
												@Override
												public void onClick(
														DialogInterface dialog,
														int which) {
													fileDir = null;

												}
											}).show();
						}
					}).setPositiveButton("取消", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {

							fileDir = null;
							System.out.println("取消》》》" + fileDir);
						}
					}).create();
			dialog.show();
		}

	}

	/**
	 * 下载
	 *
	 * @param context
	 * @param fileId
	 * @param fileName
	 */
	private static void downloadFile(final Context context, String folder,
									 final String fileDir1, String fileId, String fileName) {
		final ProgressDialog pd = ProgressDialog.show(context, "提示", "正在下载文件");

		PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
		OkHttpUtils
				.post()
				.url("http://182.92.96.58:8081/junyou/servlet/Upload")
				.addParams("action", "download")
				.addParams("folder", folder)
				.addParams("fileId", fileId)
				.addParams("fileName", fileName)
				.addParams("mobile", "true")
				.build()
				.execute(new StringCallback() {
					@Override
					public void onError(Call call, Exception e, int id) {
						pd.dismiss();
						Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT)
								.show();
					}

					@Override
					public void onResponse(String response, int id) {
						pd.dismiss();
						OutputStream os;
						try {
							os = new FileOutputStream(fileDir1);
							// 开始读取
							os.write(response.getBytes());
							os.flush();
							os.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
						Toast.makeText(context, "下载完成,存储在SD卡的“  JNLX  ”中",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static String getVersion() {
		try {
			PackageManager manager = MyApplication.getContext()
					.getPackageManager();
			PackageInfo info = manager.getPackageInfo(MyApplication.getContext().getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 获取包名
	 *
	 * @return 当前应用的包名
	 */
	public static String getAppPackageName() {
		try {
			String pkName =MyApplication.getContext().getPackageName();
			return pkName;
		} catch (Exception e) {
		}
		return null;
	}

}
